/**
 * Created by Administrator on 2017/5/11 0011.
 */

//游戏引擎
window.gameEngine = {
    ele: null,
    enemys: [],
    bullets: [],
    scoreDiv: null,

    //1.初始化引擎
    init: function () {
        this.ele = document.getElementById("main");
        return this;
    },

    //2.游戏开始
    start: function (rate) {

        this.loading(function () {
            gameEngine.score();         //创建分数窗口
            gameEngine.bulletStyle();
            //gameEngine.chooseBullet();
            myPlane.init().send(rate);  //创建我方飞机
            gameEngine.keyControl();    //调用键盘控制方法
            myPlane.drag();
            gameEngine.createEnemy();   //创建敌方飞机
            gameEngine.isCrash();       //检测碰撞

        });

    },

    //3.加载进度条
    loading: function (fn) {
        var logo = document.createElement("div");   //创建并添加 Logo
        logo.className = "logo";
        this.ele.appendChild(logo);

        var load = document.createElement("div");   //创建并添加 加载进度条
        load.className = "load";
        this.ele.appendChild(load);

        // var n = 0;
        // var timer = setInterval(function () {   //加载进度条动画
        //     load.style.backgroundImage = "url(img/loading" + (n % 3 + 1) + ".png)";
        //     n++;
        //     if (n >= 6) {
        //         clearInterval(timer);
        //         gameEngine.ele.innerHTML = "";
        //
        //         if (fn) {
        //             fn();//回调函数
        //         }
        //     }
        //console.log(n);
        //}, 400);

        var imgs = ["img/loading1.png", "img/loading2.png", "img/loading3.png"];
        var i = 0;
        var timer = setInterval(function () {
            if (i >= 5) {
                clearInterval(timer); //关闭定时器
                gameEngine.ele.innerHTML = "";

                //回调
                if (fn) {
                    fn();
                }
            } else {
                load.style.background = "url(" + imgs[(++i) % 3] + ") no-repeat";
            }
        }, 400);

    },

    //5.键盘控制方法
    keyControl: function () {

        window.onkeydown = function (e) {
            var evt = e || event;

            if (e.keyCode == 37) {
                myPlane.ele.style.left = myPlane.ele.offsetLeft - 10 + "px";
            } else if (e.keyCode == 39) {
                myPlane.ele.style.left = myPlane.ele.offsetLeft + 10 + "px";
            } else if (e.keyCode == 38) {
                myPlane.ele.style.top = myPlane.ele.offsetTop - 10 + "px";
            } else if (e.keyCode == 40) {
                myPlane.ele.style.top = myPlane.ele.offsetTop + 10 + "px";
            }

            //判断左右边界
            if (myPlane.ele.offsetLeft <= 0) {
                myPlane.ele.style.left = 0;
            } else if (myPlane.ele.offsetLeft >= gameEngine.ele.offsetWidth - myPlane.ele.offsetWidth) {
                myPlane.ele.style.left = gameEngine.ele.offsetWidth - myPlane.ele.offsetWidth + "px";
            }
            //判断上下边界
            if (myPlane.ele.offsetTop <= 0) {
                myPlane.ele.style.top = 0;
            } else if (myPlane.ele.offsetTop >= gameEngine.ele.offsetHeight - myPlane.ele.offsetHeight) {
                myPlane.ele.style.top = gameEngine.ele.offsetHeight - myPlane.ele.offsetHeight + "px";
            }
        }
    },


    //9.创建敌机 并在初始化时传参数：  [ 敌机类型css名,血量,速度,分值,爆炸时图片]
    createEnemy: function () {
        //敌机爆炸图片
        var dieImg = {
            small: ["img/plane1_die1.png", "img/plane1_die2.png", "img/plane1_die3.png"],
            mid: ["img/plane2_die1.png", "img/plane2_die2.png", "img/plane2_die3.png", "img/plane2_die4.png"],
            big: ["img/plane3_die1.png", "img/plane3_die2.png", "img/plane3_die3.png", "img/plane3_die4.png", "img/plane3_die5.png", "img/plane3_die6.png"]
        }
        //小飞机
        setInterval(function () {

            var flag = Math.random() > 0.2 ? true : false;
            if (flag) {
                new Enemy().init("enemy_small", 1, 5, 10, dieImg.small).fall();
                //console.log("111");
            }
        }, 1000);
        //中飞机
        setInterval(function () {
            var flag = Math.random() > 0.5 ? true : false;
            if (flag) {
                new Enemy().init("enemy_middle", 3, 3, 30, dieImg.mid).fall();
            }
        }, 3000);
        //大飞机
        setInterval(function () {
            var flag = Math.random() > 0.4 ? true : false;
            if (flag) {
                new Enemy().init("enemy_large", 5, 1, 50, dieImg.big).fall();
            }
        }, 6000);
    },

    //检测碰撞
    isCrash: function () {
        //检测子弹是否与敌机碰撞
        setInterval(function () {
            for (var i in gameEngine.enemys) {
                for (var j in gameEngine.bullets) {
                     if (isCrash(gameEngine.bullets[j].ele, gameEngine.enemys[i].ele)) {
                        gameEngine.bullets[j].boom();   //碰撞后子弹爆炸
                        delete gameEngine.bullets[j];
                        gameEngine.enemys[i].hurt();    //碰撞后敌机掉血
                    }
                }
            }

            //检测我方飞机与敌机是否相撞
            for (var k in gameEngine.enemys) {

                if (isCrash(gameEngine.enemys[k].ele, myPlane.ele)) {
                    gameEngine.enemys[k].boom();
                    myPlane.boom();



                    return;
                }
            }
        }, 50);
    },

    //创建分数框
    score: function () {
        this.scoreDiv = document.createElement("div");
        gameEngine.ele.appendChild(this.scoreDiv);
        this.scoreDiv.className = "score";
        this.scoreDiv.innerHTML = 0;
    },

    //创建子弹模式选项
    bulletStyle:function () {
        this.bulletDiv = document.createElement("div");
        gameEngine.ele.appendChild(this.bulletDiv);
        this.bulletDiv.innerHTML = '<input type="button" value="单发"/><input type="button" value="散弹"/><input type="button" value="蛇弹"/><input type="button" value="随机"/>';
        this.bulletDiv.className = "bulletDiv";
    },

    //选择模式
    // chooseBullet:function () {
    //     var aInput = gameEngine.bulletDiv.getElementsByTagName("input");
    //     for(var i = 0; i < aInput.length; i++){
    //         aInput[i].index = i;
    //         aInput[i].onclick = function () {
    //             console.log(this.index);
    //             return this.index;
    //
    //         }
    //     }
    // }
}

//
